#!/usr/bin/python
# -*- encoding: utf8 =*-

class SimpleHtmlParse():
	def handle_starttag(self, tag, attrs):
		pass
	def handle_data(self, data):
		pass
	def handle_endtag(self):
		pass
	def feed(self, html):
		data = ''
		tagState = 0	# 0: out of <>
						# 1: in the <>
						# 2: in the </>
		refState = False	# False: Out of ""
							# True: in the ""
		prev = ''
		for char in html:
			if prev == '\\':
				data += char
				prev = char
				continue
				
			if char == '"' and tagState != 0:
				if refState:
					refState = False
				else:
					refState = True
				prev = char
				continue
			elif tagState == 0 and refState == False and char == '<':
				if data != '':
					self.handle_data(data)
					data = ''
				tagState = 1
				prev = char
				continue
			elif tagState == 1 and refState == False and char == '/':
				tagState = 2
				prev = char
				continue
			elif tagState != 0 and refState == False and char == '>':
				#print ("%d %s" % (tagState, data))
				if tagState == 1:
					tag = data.strip()
					words = tag.split()
					attrs = []
					if len(words):
						tag = words[0]
						words.pop(0)
					for w in words:
						attrs.append(w.split('='))
					self.handle_starttag(tag, attrs)
				else: # tagState == 2:
					self.handle_endtag(data.strip())
				data = ''
				tagState = 0
				prev = char
				continue
			else:
				data += char
				prev = char
